import Global from '../Model/Global/GCPublic';
import GCStorage from "../Model/DataTool/GCStorage";

/*
 * 首页
 */
// 获取车辆列表
export function GetCarList(state, action) {
    let D_CarList;
    if (state.D_CarList_InNext!==0&&action.now_page!==1) {
        D_CarList = state.D_CarList.concat(action.result.msg_list)
    } else {
        D_CarList = action.result.bus_list;
    }
    return Object.assign({}, state, {
        D_CarList: D_CarList,
        D_CarList_Page: action.result.now_page,
        D_CarList_InNext: action.result.is_next,
        D_CarList_IsRefreshing: false,
    });
}

// 查询营收、订单、车辆
export function GetHomeInfo(state, action) {
    return Object.assign({}, state, {
        D_HomeInfo: action.result,
    });
}

/*
 * 统计
 */
// 经营分析
export function QueryAnalysis(state, action) {
    let query_analysis;
    if (state.D_QueryAnalysis_InNext!==0&&action.now_page!==1) {
        query_analysis = state.D_QueryAnalysis.concat(action.result.line_list)
    } else {
        query_analysis = action.result.line_list;
    }
    return Object.assign({}, state, {
        D_QueryAnalysis: query_analysis,
        D_QueryAnalysis_Page: action.result.now_page,
        D_QueryAnalysis_InNext: action.result.is_next,
        D_QueryAnalysis_IsRefreshing: false,
    });

}

// 营业统计
export function QueryBusiness(state, action) {
    return Object.assign({}, state, {
        D_QueryBusiness: action.result,
        D_QueryBusiness_IsRefreshing:true,
    });
}

/*
 * 用户中心
 */
// 关于我们
export function AboutUs(state, action) {
    return Object.assign({}, state, {
        D_AboutUs: action.result,
    });
}
// 消息列表
export function GetMessageList(state, action) {
    let D_MsgList;
    if (state.D_MsgList_InNext!==0&&action.now_page!==1) {
        D_MsgList = state.D_MsgList.concat(action.result.msg_list)
    } else {
        D_MsgList = action.result.msg_list;
    }
    return Object.assign({}, state, {
        D_MsgList: D_MsgList,
        D_MsgList_Page: action.result.now_page,
        D_MsgList_InNext: action.result.is_next,
        D_MsgList_IsRefreshing: false,
    });

}
// 账号密码登录
export function Login(state, action) {
    if (action.result == '') {
        return;
    }
    GCStorage.save(
        Global.Storage.storageUser,
        {
            id: action.result.id,
            name: action.result.name,
            token: action.result.token,
            mobile: action.result.mobile,
            business_licence: action.result.business_licence,
            certificate: action.result.certificate,
            corporate_bankno: action.result.corporate_bankno,
        }
    );
    Global.User.id = action.result.id;
    Global.User.name = action.result.name;
    Global.User.token = action.result.token;
    Global.User.user_name = action.result.name;
    Global.User.mobile = action.result.mobile;
    Global.User.business_licence = action.result.business_licence;
    Global.User.certificate = action.result.certificate;
    Global.User.corporate_bankno = action.result.corporate_bankno;
    return Object.assign({}, state, {
        id: action.result.id,
        name: action.result.name,
        token: action.result.token,
        mobile: action.result.mobile,
        business_licence: action.result.business_licence,
        certificate: action.result.certificate,
        corporate_bankno: action.result.corporate_bankno,
        isLoggedIn: true,
        isLoggedOut: false,
    });
}

export function UserFromStorage(state, action) {
    return {
        ...state,
        ...action
    };
}

export function UserLogout(state, action) {
    Global.User.token = '';
    Global.User.id = '';
    Global.User.name = '';
    Global.User.mobile = '';
    Global.User.business_licence = '';
    Global.User.certificate = '';
    Global.User.corporate_bankno = '';
    return {
        ...state,
        ...action
    };
}
